/**
 * 
 */
package cn.biplam.back.bean.tree;

import java.util.ArrayList;
import java.util.Map;

import cn.biplam.back.bean.mdmanage.IndexTableImp;
import cn.biplam.common.bean.tree.IndexTableExt;
import cn.biplam.common.bean.tree.IndexTableSimple;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.DbResource;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.SysManageCode.TreeIndexCode;
import cn.biplam.common.struc.BaseStruc.IndexTableStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;


/**
 * 索引表扩展操作
 * @author 	WangSong 2009-11-25 下午02:16:36
 * @version 1.0
 */
@SuppressWarnings("serial")
public class IndexTableExtImpl extends IndexTableExt {
	
	public IndexTableExtImpl(String userId,String tableName){
		this.userId=userId;
		this.tableName=tableName;
	}

	@Override
	public OperatorResult childHaveChild(String nodeId) throws ErrorMessage {
		boolean have=false;
		//判断索引表,该主题是否有下级
		DataFilter filter = new DataFilter();
		filter.put("table_name", this.tableName);
		filter.put(IndexTableStruc.NODE_ID, nodeId);
		OperatorResult rs = Operator.get(TreeIndexCode.Get_Immediate_Children, userId, filter);
		OperatorResult rsCurr;
		if(rs.size()>0){
			do{
				filter.put(IndexTableStruc.NODE_ID, rs.getValue(IndexTableStruc.NODE_ID));
				rsCurr = Operator.get(TreeIndexCode.Get_Immediate_Children, userId, filter);
				if(TOOLS.exist(rsCurr)){
					have=true;
					break;
				}
			}while(rs.next());
		}else{
			have=false;
		}
		rs.setDmlResult(true);
		rs.setBooleanResult(have);
		return rs;
	}

	@Override
	public OperatorResult copyNode(String srcTableName, String srcNodeId,
			String parentNodeId, boolean includeSubNode, boolean getSql)
			throws ErrorMessage {
		throw new ErrorMessage("本功能尚未实现");
	}

	/**
	 *  删除节点及其所有儿子<BR>
	 *  <font COLOR='red'>适用于儿子节点个数不是很多的情况(儿子节点ID的字符串总长度不超过1000)</font>
	 * @param nodeId 节点ID
	 * @param getSql 是否返回SQL
	 * @return getSql:true  删除所有下级节点的SQL<BR>
	 *         getSql:false 删除是否成功<BR>
	 * @throws ErrorMessage
	 */
	@Override
	public OperatorResult delAllChildren(String nodeId, boolean getSql)
			throws ErrorMessage {
		ArrayList<String> sql = new ArrayList<String>();
		//1.查询出所有下级的node_id
		ArrayList<String> list = getChilds(nodeId, dbResource, null);
		//2.生成删除所有孩子的SQL
		IndexTableSimple indexOpt = IndexTableSimple.getNewInstance(userId, this.tableName);
		if(list!=null && list.size()>0){
			for(String child: list){
				sql.addAll(indexOpt.delete(child, true).getSqls());
			}
		} 
		//删除当前节点SQL
		sql.addAll(indexOpt.delete(nodeId,true).getSqls());
		
		if(getSql){
			this.setSql(sql);
			return this.result;//返回SQL
		}else{
			return this.doingDML(sql);//删除后返回结果
		}
	}

	/**
	 *  获取下级节点列表<BR>可用于sql in子句的列表('a','b')
	 * @param nodeId 节点ID
	 * @return 返回下级节点的列表(逗号分隔)<BR>
	 * @throws ErrorMessage
	 */
	@Override
	public String getChildrenList(String nodeId) throws ErrorMessage{
		return this.getChildrenList(nodeId, null,null);
	}
	

	/**
	 *  获取下级节点列表<BR>可用于sql in子句的列表('a','b')
	 * @param nodeId 节点ID
	 * @param dbResource 数据源(可以为空,如果为空连接到默认数据源)
	 * @param filterValues 字符类型过滤条件 (Key:字段名;Value:字段值)
	 * @return 返回下级节点的列表(逗号分隔)<BR>
	 * @throws ErrorMessage
	 */
	@Override
	public String getChildrenList(String nodeId, 
			DbResource dbResource,
			KvMap<String, String> filterValues) throws ErrorMessage{
		if(dbResource == null)dbResource = new DbResource();
		FindChildren find = new FindChildren(dbResource,
				tableName,
				IndexTableStruc.NODE_ID,
				IndexTableStruc.NODE_PARENT,
				nodeId);
		//设置过滤条件
		if(filterValues!=null){
			for(Map.Entry<String, String> filter: filterValues){
				find.putFilter(filter.getKey(), filter.getValue());
			}
		}
		//返回下级列表
		find.start();
		return find.getSqlList();
	}
	
	@Override
	public String getChildrenCodeList(String nodeId, 
			DbResource dbResource,
			KvMap<String, String> filterValues) throws ErrorMessage{
		if(dbResource == null)dbResource = new DbResource();
		FindChildren find = new FindChildren(dbResource,
				tableName,
				IndexTableStruc.NODE_ID,
				IndexTableStruc.NODE_PARENT,
				nodeId,
				IndexTableStruc.NODE_CODE
				);
		//设置过滤条件
		if(filterValues!=null){
			for(Map.Entry<String, String> filter: filterValues){
				find.putFilter(filter.getKey(), filter.getValue());
			}
		}
		//返回下级列表
		find.start();
		return find.getSqlList();
	}
	
	/**
	 *  获取下级节点列表<BR>
	 * @param nodeId 节点ID
	 * @param dbResource 数据源(可以为空,如果为空连接到默认数据源)
	 * @param filterValues 字符类型过滤条件 (Key:字段名;Value:字段值)
	 * @return  返回下级节点的列表
	 */
	public ArrayList<String> getChilds(String nodeId,
			DbResource dbResource,
			KvMap<String, String> filterValues) throws ErrorMessage{
		if(dbResource == null)dbResource = new DbResource();
		FindChildren find = new FindChildren(dbResource,
				tableName,
				IndexTableStruc.NODE_ID,
				IndexTableStruc.NODE_PARENT,
				nodeId);
		//设置过滤条件
		if(filterValues!=null){
			for(Map.Entry<String, String> filter: filterValues){
				find.putFilter(filter.getKey(), filter.getValue());
			}
		}
		//返回下级列表
		find.start();
		return find.getNodeList();
	}
	
	 /**  查找两个节点之间的路径
	 * @param startNode 起始节点
	 * @param endNode   终止节点
	 * @return  终止节点为空时返回起始节点到根节点的路径<BR>
	 * 		   当起始节点或者终止节点不存在时:返回NULL<BR>
	 *         起始节点和终止节点间不存在路径时:返回null<BR>
	 *         终止节点开始到起始节点间起始节点的所有父亲(包含终止节点)<BR>
	 */
	@Override
	public ArrayList<String> findPath(String startNode, String endNode) throws ErrorMessage {
		IndexTableImp indexTable = new IndexTableImp(userId,this.tableName);
		return indexTable.findPath(startNode, endNode);
	}
}
